package cn.hwadee.muying.service;

import cn.hwadee.muying.dto.Worker;
import cn.hwadee.muying.entity.Contract;
import cn.hwadee.muying.entity.WorkerInfo;
import cn.hwadee.muying.mapper.ContractMapper;
import cn.hwadee.muying.commons.base.BaseService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.stereotype.Service;
import cn.hwadee.muying.entity.User;
import cn.hwadee.muying.commons.page.MyPage;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务合同 服务实现类
 * </p>
 *
 * @author Mankind
 * @since 2018-08-06
 */
@Service
public class ContractService extends BaseService<ContractMapper, Contract> {

    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private WorkerService workerService;

    public MyPage<Contract> list(MyPage<Contract> page, User cUser, Long userId, Long nurseryTeacherId, Long nurseryConsultantId, Date startDate, Date endDate, Long status, String keyword) throws Exception{
        List<Contract> contracts = contractMapper.list(page,cUser,userId,nurseryTeacherId,nurseryConsultantId,startDate,endDate,status,keyword);
        for(Contract contract:contracts){
            User user=userService.selectById(contract.getUserId());
            if(user!=null){
                contract.setUserName(user.getUsername());
            }
            user=userService.selectById(contract.getNurseryTeacherId());
            if(user!=null){
                contract.setNurseryTeacherName(user.getUsername());
            }
            user=userService.selectById(contract.getNurseryConsultantId());
            if(user!=null){
                contract.setNurseryConsultantName(user.getUsername());
            }
        }
        page.setRecords(contracts);
        return page;
    }

    public void finishContract(Long conId) throws Exception{
        contractMapper.finishContract(conId);
    }
    public List<Contract> selectAll() {
        return contractMapper.selectList(
                new EntityWrapper<Contract>()
                        .and("valid=1")
        );
    }


    public List<Contract> selectByOrgId(Long orgId) {
        List<Contract> contracts=contractMapper.selectList(
                new EntityWrapper<Contract>()
                        .and("org_id="+orgId)
                        .and("valid=1")
        );
        for(Contract contract:contracts){
            User user=userService.selectById(contract.getUserId());
            if(user!=null){
                contract.setUserName(user.getUsername());
            }
        }
        return contracts;
    }
    public List<Contract> selectByUserId(Long id) {
        List<Contract> contractList= contractMapper.selectList(
                new EntityWrapper<Contract>()
                        .and("user_id="+id)
                        .and("valid=1")
        );
        for(Contract contract:contractList){
            Worker worker=workerService.selectById(contract.getNurseryTeacherId());
            if(worker!=null){
                contract.setWorkerLevel(worker.getWorkerLevel());
                contract.setNurseryTeacherName(worker.getUsername());
            }
            User user=userService.selectById(contract.getNurseryConsultantId());
            if(user!=null){
                contract.setNurseryConsultantName(user.getUsername());
            }
        }

        return contractList;
    }

    public List<User> selectReturnVisitList(Long orgId,Long rid) {
        return contractMapper.selectReturnVisitList(orgId,rid);
//        List<Contract> contractList=this.selectByOrgId(orgId);
//        for(Contract contract:contractList){
//            User user=null;
//            if(contract.getNurseryConsultantId()!=null){
//                user=userService.selectById(contract.getNurseryTeacherId());
//                if(user!=null){
//                    userList.add(user);
//                }
//            }
//            if(contract.getNurseryTeacherId()!=null){
//                user=userService.selectById(contract.getNurseryTeacherId());
//                if(user!=null){
//                    userList.add(user);
//                }
//            }
//        }
    }
}
